package com.itheima.demo;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class Demo01 {//数据连接池练习

    public static void main(String[] args) throws Exception {
        //showData();
        //addData();
        //updateData();
        //deleteData();
    }

    //删除指定品牌
    private static boolean deleteData() throws Exception{
        //要删除的品牌ID
        Integer id = 4;
        //加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("day001\\src\\druid.properties"));
        //创建数据连接池
        DataSource pool = DruidDataSourceFactory.createDataSource(prop);
        //创建数据库连接
        Connection conn = pool.getConnection();
        //定义sql语句
        String sql = "delete from tb_brand where id = ?";
        //获取预执行对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //传递参数
        preparedStatement.setInt(1,id);
        //执行sql
        int count = preparedStatement.executeUpdate();
        //释放资源
        preparedStatement.close();
        conn.close();
        //处理结果
        return count>0;
    }

    //添加品牌
    private static boolean updateDate() throws Exception {
        //添加品牌信息
        String brandName = "香飘飘";
        String companyName = "香飘飘大公司";
        Integer ordered = 600;
        String description = "香飘飘可绕地球三圈";
        Integer status = 1;
        Integer id = 4;
        //加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("day001\\src\\druid.properties"));
        //创建数据连接池
        DataSource pool = DruidDataSourceFactory.createDataSource(prop);
        //创建数据库连接
        Connection conn = pool.getConnection();
        //定义sql语句
        String sql = "update tb_brand set brand_name = ?,company_name= ?,ordered= ?,description= ?,status= ? where id = ?";
        //获取预执行对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //传递参数
        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);
        preparedStatement.setInt(6,id);
        //执行sql
        int count = preparedStatement.executeUpdate();
        //释放资源
        preparedStatement.close();
        conn.close();
        //处理结果
        return count>0;
    }

    //添加品牌
    private static boolean addData() throws Exception {
        //添加品牌信息
        String brandName = "香飘飘";
        String companyName = "香飘飘公司";
        Integer ordered = 60;
        String description = "香飘飘可绕地球一圈";
        Integer status = 1;
        //加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("day001\\src\\druid.properties"));
        //创建数据连接池
        DataSource pool = DruidDataSourceFactory.createDataSource(prop);
        //创建数据库连接
        Connection conn = pool.getConnection();
        //定义sql语句
        String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?);";
        //获取预执行对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //传递参数
        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);
        //执行sql
        int count = preparedStatement.executeUpdate();
        //释放资源
        preparedStatement.close();
        conn.close();
        //处理结果
        return count>0;
    }

    //查询所有品牌
    private static void showData() throws Exception {
        //加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("day001\\src\\druid.properties"));
        //创建数据连接池
        DataSource pool = DruidDataSourceFactory.createDataSource(prop);
        //创建数据库连接
        Connection conn = pool.getConnection();
        //定义sql语句
        String sql = "select * from tb_brand";
        //预执行
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //传递参数

        //执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        //处理结果
        Brand brand = null;
        ArrayList<Brand> list = new ArrayList<>();
        while (resultSet.next()) {
            brand = new Brand();

            brand.setId(resultSet.getInt("id"));
            brand.setBrandName(resultSet.getString("brand_name"));
            brand.setCompanyName(resultSet.getString("company_name"));
            brand.setDescription(resultSet.getString("description"));
            brand.setOrdered(resultSet.getInt("ordered"));
            brand.setStatus(resultSet.getInt("status"));

            list.add(brand);
        }

        System.out.println(list);
        //释放资源
        resultSet.close();
        preparedStatement.close();
        conn.close();
    }


}
